import {
  loadUsersJson,
  saveUser,
  deleteUser,
  editUserInfoNew,
  resetUserPassword,
} from '@/services/geographic';
import { queryCurrent } from '@/services/user';
export default {
  namespace: 'management',
  state: {
    total: 0,
    items: [],
    loading: true,
  },
  effects: {
    *getUsers({ payload, callback }, { call, put }) {
      const response = yield call(loadUsersJson, payload);
      if (response) {
        if (callback && typeof callback === 'function') {
          callback(response);
        }
        yield put({
          type: 'save',
          payload: response,
        });
      }
    },
    *addUser({ payload, callback }, { call, put }) {
      const response = yield call(saveUser, payload);
      if (response) {
        if (callback && typeof callback === 'function') {
          callback(response);
        }
        yield put({
          type: 'save',
          payload: response,
        });
      }
    },
    *editUser({ payload, callback }, { call, put }) {
      const response = yield call(editUserInfoNew, payload);
      if (response) {
        if (callback && typeof callback === 'function') {
          callback(response);
        }
        yield put({
          type: 'save',
          payload: response,
        });
      }
    },
    *resetUser({ payload, callback }, { call, put }) {
      const response = yield call(resetUserPassword, payload);
      if (response) {
        if (callback && typeof callback === 'function') {
          callback(response);
        }
        yield put({
          type: 'save',
          payload: response,
        });
      }
    },
    *delUser({ payload, callback }, { call, put }) {
      const response = yield call(deleteUser, payload);
      if (response) {
        if (callback && typeof callback === 'function') {
          callback(response);
        }
        yield put({
          type: 'save',
          payload: response,
        });
      }
    },
    *queryUser({ payload, callback }, { call, put }) {
      const response = yield call(queryCurrent, payload);
      if (response) {
        if (callback && typeof callback === 'function') {
          callback(response);
        }
        yield put({
          type: 'save',
          payload: response,
        });
      }
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
